package hrms.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import hrms.dao.DepartmentMapper;
import hrms.dao.PostMapper;
import hrms.dao.RecruitmentMapper;
import hrms.dao.StaffMapper;
import hrms.entity.*;
import hrms.util.PageRequest;
import hrms.util.PageResult;
import hrms.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

@Service
public class RecruitmentService {

    @Autowired
    RecruitmentMapper recruitmentMapper;
    @Autowired
    DepartmentMapper departmentMapper;
    @Autowired
    StaffMapper staffMapper;
    @Autowired
    PostMapper postMapper;

    public Recruitment findById(Integer id){ return recruitmentMapper.findById(id);}

    public List<Recruitment> findAllByDepartmentId(Integer id){
        return recruitmentMapper.findAllByDepartmentId(id);
    }

    public List<Recruitment> findAll(){
        return recruitmentMapper.findAll();
    }

    public void insertRecruitment(Recruitment recruitment){
        recruitmentMapper.insertRecruitment(recruitment);
    }

    public void updateRecruitment(Recruitment recruitment){
        recruitmentMapper.updateRecruitment(recruitment);
    }

    public PageResult findPage(PageRequest pageRequest) {
        return PageUtils.getPageResult(pageRequest, getPageInfo(pageRequest));
    }

    private PageInfo<Recruitment> getPageInfo(PageRequest pageRequest) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<Recruitment> sysMenus = recruitmentMapper.findByPage();
        return new PageInfo<Recruitment>(sysMenus);
    }

    public List<Recruitment> findStatus0(){
        List<Recruitment> all = recruitmentMapper.findStatus0();
        for (Recruitment recruitment : all) {
            Department department = departmentMapper.findById(recruitment.getDepartment().getId());
            recruitment.setDepartment(department);
            Post post = postMapper.findById(recruitment.getPost().getId());
            recruitment.setPost(post);
            Staff staff= staffMapper.findById(recruitment.getStaff().getId());
            recruitment.setStaff(staff);
        }
        return all;
    }

    public void updateQuantity(Recruitment recruitment){
        recruitmentMapper.updateQuantity(recruitment);
    }

    public void updateStatus(Recruitment recruitment){
        recruitmentMapper.updateStatus(recruitment);
    }

}
